const {
  ScanCodeModel
} = require('../../../utils/scancode-model.js')
const scanCodeModel = new ScanCodeModel()
const {
  MemberModel
} = require('../../../utils/member-model.js');
var memberModel = new MemberModel();
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: true,
    phoneShow: false,
    encryptedDataUser: '',
    ivUser: '',
    scene: "", //场景id
    formId: '',
    paramInfo: "", //跳转参数
    goUrl: "", //跳转地址
    operatorId: '',
    source: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      // scene: options.scene || 'saas-rc3-2249',
      scene: options.scene
    })
    app.getInfo().then(data => {
      that.getQrcodeParse();
    })
  },

  /**
   * 解析二维码
   */
  getQrcodeParse() {
    var that = this;
    var param = {};
    param.scene = that.data.scene; //场景id
    param.merchantId = wx.getStorageSync('merchantId'); //商户id
    scanCodeModel.qrcodeParse(param).then(data => {
      var itemParam = data.info || "{}"; //参数
      var url = ``;
      const info = JSON.parse(itemParam)
      let source = ''
      if (data.actionType == 1) {
        url = `/pages/scanCode/mobileCashier/index?itemParam=${encodeURIComponent(itemParam)}`
        source = 4
      } else if (data.actionType == 2) {
        url = `/pages/games/redPacket/index?shareMemberId=${info.shareMemberId}&activityId=${info.activityId}&activityName=${info.activityName}&orderCode=${info.orderCode}`
      } else if (data.actionType == 3) {
        // url = `/pages/scanCode/firmware/chooseMeal/index?machineInstanceSlotId=${info.machineInstanceSlotId}`
        url = `/pages/scanCode/firmware/property/index?machineInstanceSlotId=${info.machineInstanceSlotId}`
        source = 6
      } else if (data.actionType == 4) {
        url = `/pages/games/scratchCard/index?activityType=${info.activityType}&gameConfigName=${info.activityName}&id=${info.id}`
      } else if (data.actionType == 5) {
        url = `/pages/games/bigWheel/index?activityType=${info.activityType}&gameConfigName=${info.activityName}&id=${info.id}`
      } else if (data.actionType == 6) {
        url = `/pages/games/goldenEggs/index?activityType=${info.activityType}&gameConfigName=${info.activityName}&id=${info.id}`
      } else if (data.actionType == 9) {
        url = `${data.url}?source=${info.source}&shareChannel=${info.shareChannel}&operatorId=${info.operatorId}&shopId=${info.shopId}`
      }
      that.setData({
        source,
        goUrl: url,
        operatorId: info.operatorId || ""
      }, function () {
        if (data.actionType == 9) {
          if (app.globalData.isLogin) {
            wx.redirectTo({
              url: '/pages/index/index'
            })
          } else {
            wx.redirectTo({
              url
            })
          }
        } else {
          that.goHtmlUrl();
        }
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 跳转地址
   */
  goHtmlUrl() {
    var that = this;
    if (app.globalData.isLogin) {
      that.setData({
        isLogin: true
      })
      wx.redirectTo({
        url: that.data.goUrl
      })
    } else {
      that.setData({
        isLogin: false
      })
    }
  },

  formSubmit1(e) {
    this.setData({
      formId: e.detail.formId
    })
  },

  getUserInfo({
    detail
  }) {
    const {
      encryptedData,
      iv
    } = detail
    if (encryptedData) {
      this.setData({
        encryptedDataUser: encryptedData,
        ivUser: iv,
        phoneShow: true
      })
    }
  },
  getOrderInfo() {
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    this.setData({
      isLogin: true
    })
    if (this.data.formId) {
      memberModel.userMessage({
        merchantId: wx.getStorageSync('merchantId'),
        formIds: [this.data.formId]
      })
    }
    this.goHtmlUrl();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})